
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <%
        pageContext.setAttribute("APP_PATH", request.getContextPath()); //teacher_science_system/
        //        pageContext.setAttribute("APP_PATH", "teacher_science_system/");
    %>
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</head>
<body>
<jsp:include page="admin_header.jsp"/>

<div class="container">
    <div class="row">
        <div class="col-md-12">
            <h1>管理著作</h1>
        </div>
    </div>
    <div class="row">
        <div class="col-md-4 col-md-offset-8">
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <table class="table table-hover" id = "managerWorks_table">
                <thead>
                <tr>
                    <th>刊物编码</th>
                    <th>作者ID</th>
                    <th>著作名称</th>
                    <th>出版社</th>
                    <th>出版时间</th>
                    <th>编者</th>
                    <th>当前状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>

                </tbody>

            </table>
        </div>
    </div>
    <div class="row">
        <div class="col-md-6" id = "page_info_area">
        </div>
        <div class="col-md-6" id = "page_nav_area">
        </div>
    </div>
</div>
<script type = "text/javascript">

    //1、页面加载完成以后，直接去发送一个ajax请求，要到分页数据
    $(function(){
        //去首页
        to_page(1,"/seeAllWorks");
    });

    function to_page(pn,road) {
        $.ajax({
            url:"${APP_PATH}"+road,
            data:"pn=" + pn,
            type:"POST",
            success:function(result){
                //1、解析并显示数据
                build_check_paper(result);
                //2、解析并显示分页数据
                build_page_info(result);
                //3、解析显示分页条数据
                build_page_nav(result,road);
            }
        });
    }
    //1、解析并显示数据
    function  build_check_paper(result) {

        //清空table表格
        $("#managerWorks_table tbody").empty();
        var  emps = result.data.list;

        $.each(emps,function (index,item) {
            // 后台list的各个属性
            var resultUUIdTd = $("<td></td>").append(item.resultuuid);
            var teacherId = $("<td></td>").append(item.teacherId);
            var worksname = $("<td></td>").append(item.worksname);
            var workspress = $("<td></td>").append(item.workspress);
            var worksdate = $("<td></td>").append(item.worksdate);
            var workseditor = $("<td></td>").append(item.editor);
            var stateTd = $("<td></td>");

            if(item.checkflag==1){
                stateTd.append("审核通过");
            }else if(item.checkflag==3){
                stateTd.append("已被下架");
            }else if(item.checkflag==0){
                stateTd.append("未经过审核");
            }
            var subBtn = $("<button></button>").addClass("btn btn-success btn-sm sub_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-ok"))
                .append("通过");
            subBtn.attr("id",item.resultuuid);

            var failBtn =$("<button style='margin-top:4px;margin-left:4px;'></button>").addClass("btn btn-danger btn-sm fail_btn")
                .append($("<span></span>").addClass("glyphicon glyphicon-pencil")
                    .append("下架"));

            failBtn.attr("id",item.resultuuid);

            if(item.checkflag==3 || item.checkflag==0){

                var btnTd = $("<td></td>").append(subBtn);

            }else if(item.checkflag==1){  //审核通过
                var btnTd = $("<td></td>").append(failBtn);
            }
            $("<tr></tr>")
                .append(resultUUIdTd)
                .append(teacherId)
                .append(worksname)
                .append(workspress)
                .append(worksdate)
                .append(workseditor)
                .append(stateTd).
                append(btnTd).appendTo("#managerWorks_table tbody")
        });
    }
    //解析显示分页信息
    function build_page_info(result) {
        $("#page_info_area").empty();
        $("#page_info_area").append("当前第"+result.data.pageNum+
            "页，总共"+result.data.pages+"页，总共"+result.data.total +"条记录。");

    }
    //解析显示分页条，点击分页要能去下一页
    function build_page_nav(result,road) {
        $("#page_nav_area").empty();
        var ul = $("<ul></ul>").addClass("pagination");
        //构建元素
        var firstPageLi = $("<li></li>").append($("<a></a>").append("首页").attr("href","#"));
        var prePageLi = $("<li></li>").append($("<a></a>").append("&laquo;"));
        if(result.data.hasPreviousPage == false){
            firstPageLi.addClass("disabled");
            prePageLi.addClass("disabled");

        }else{
            //为元素添加点击翻页的事件
            firstPageLi.click(function(){
                to_page(1,road);
            });

        }prePageLi.click(function(){
            to_page(result.data.pageNum-1,road);
        });
        var nextPageLi = $("<li></li>").append($("<a></a>").append("&raquo;"));
        var lastPageLi = $("<li></li>").append($("<a></a>").append("末页").attr("href","#"));
        if(result.data.hasNextPage == false){
            nextPageLi.addClass("disabled");
            lastPageLi.addClass("disabled");
        }else{
            nextPageLi.click(function () {
                to_page(result.data.pageNum+1,road);
            });
            lastPageLi.click(function () {
                to_page(result.data.pages,road);
            });
        }

        //添加首页和前一页的提示
        ul.append(firstPageLi).append(prePageLi);
        //1,2,3,4,5,遍历给ul中添加页码提示
        $.each(result.data.navigatepageNums,function (index,item) {

            var numLi = $("<li></li>").append($("<a></a>").append(item));
            if(result.data.pageNum==item){
                numLi.addClass("active");
            }
            numLi.click(function(){
                to_page(item,road);
            });
            ul.append(numLi);
        });
        //添加下一页和末页的提示
        ul.append(nextPageLi).append(lastPageLi);
        //把ul加入到nav
        var navEle = $("<nav></nav>").append(ul);
        navEle.appendTo("#page_nav_area");
    }

    //通过审核
    $(document).on("click",".sub_btn",function() {
        // var val = $(this).attr("id");
        check($(this).attr("id"), 1, "/reUpdateWorks");

    });

    //下架
    $(document).on("click",".fail_btn",function() {
        check($(this).attr("id"), 3, "/reUpdateWorks");
    });

    function check(worksId, opeCode,road){
        $.ajax({
            url:"${APP_PATH}" + road,
            data:{"worksId":worksId+"", "operaCode":opeCode},
            type:"GET",
            success:function(){
                to_page(1,"/seeAllWorks");
            }
        });
    }
</script>

<jsp:include page="admin_foot.jsp"/>
</body>
</html>
